var dp:Array;
var pub_ns:NetStream;

function getSurveyItem(item:Object):String
{
	//trace("getSurveyItem...id:"+item.id);
	var cont:String="<moonlight name='SurveyPaper' template='SurveyPaper-container' uri='app/SurveyPaper.xml'>"
	+"<portlet name="+"'item_"+item.id+"' drag='no' height='50'>";	
	cont+="<textfield name='question_"+item.id+"' x='5' y='1' html='true' selectable='no' width='650' height='20'>"+item.id+". "+item.question+"</textfield>";
	var x:Number=25;
	var delta:Number=Math.floor(600/item.answerCount);
	delta=delta>150?150:delta;
	for (var a in item.answer)
	{
		cont+="<media x='"+x+"' y='24' name='checkbox_"+item.id+"_"+a+"' src='app/checkbox.png' type='image' />";
		if (item.answer[a].selected)
			cont+="<media x='"+x+"' y='22' name='check_"+item.id+"_"+a+"' src='app/check.png' type='image' alpha='100'/>";
		else
			cont+="<media x='"+x+"' y='22' name='check_"+item.id+"_"+a+"' src='app/check.png' type='image' alpha='0'/>";
		
		cont+="<hotspot x='"+x+"' y='22' name='checkit_"+item.id+"_"+a+"'  alpha='0' width='15' height='15' />"
			+"<textfield x='"+Number(x+20)+"' y='20'"+" name='answer_"+item.id+"_"+a+"' html='true' selectable='no' width='150' height='20'>"+item.answer[a].label+"</textfield>"
			+"<handler ins='SurveyPaper-checkit_"+item.id+"_"+a+"' type='onPress' value='check' src='lib:notes.components.SurveyPaper.check'/>";		
		
		x+=delta;
	}
	cont+="</portlet></moonlight>";
	//trace("cont..."+cont);
	return cont;
}

function getSurveyResultItem(item:Object, type:String):String
{
	//trace("getSurveyResultItem...id:"+item.id);
	var cont:String="<moonlight name='SurveyResult' template='SurveyResult-container' uri='app/SurveyResult.xml'>"
	+"<portlet name="+"'item_"+item.id+"' drag='no' height='70'>";	
	cont+="<textfield name='question_"+item.id+"' x='5' y='1' html='true' selectable='no' width='650' height='20'>"+item.id+". "+item.question+"</textfield>";
	cont+="<media x='600' y='4' name='query_"+item.id+"' src='app/search.png' type='image' />"
		+"<handler ins='SurveyResult-query_"+item.id+"' type='onPress' value='queryItem' src='lib:notes.components.SurveyResult.queryItem'/>";

	var x:Number=25;
	var delta:Number=Math.floor(600/item.answerCount);
	delta=delta>150?150:delta;
	for (var a in item.answer)
	{
		cont+="<media x='"+x+"' y='24' name='checkbox_"+item.id+"_"+a+"' src='app/checkbox.png' type='image' />";
		if (item.answer[a].selected)
			cont+="<media x='"+x+"' y='22' name='check_"+item.id+"_"+a+"' src='app/check.png' type='image' alpha='100'/>";
		else
			cont+="<media x='"+x+"' y='22' name='check_"+item.id+"_"+a+"' src='app/check.png' type='image' alpha='0'/>";
		
		cont+="<hotspot x='"+x+"' y='22' name='checkit_"+item.id+"_"+a+"'  alpha='0' width='15' height='15' />"
			+"<textfield x='"+Number(x+20)+"' y='20'"+" name='answer_"+item.id+"_"+a+"' html='true' selectable='no' width='150' height='20'>"+item.answer[a].label+"</textfield>"
			+"<handler ins='SurveyPaper-checkit_"+item.id+"_"+a+"' type='onPress' value='check' src='lib:notes.components.SurveyResult.check'/>";		

		cont+="<textfield x='"+Number(x+20)+"' y='40'"+" name='count_"+item.id+"_"+a+"' html='true' selectable='no' width='150' height='20'></textfield>";
		x+=delta;
	}
	cont+="</portlet></moonlight>";
	//trace("cont..."+cont);
	return cont;
}

function publish(uri:String)
{
	trace("publish...uri:"+uri);
	if (pub_ns==null)
	{
		var nc:NetConnection=Packages.system.MLF.accessDefaultConnection();
		if (nc!=null)	pub_ns=new NetStream(nc);
	}
	var active_mic:Microphone=Microphone.get();
	pub_ns.attachAudio(active_mic);
	pub_ns.publish(uri, "append");	//record?
}

function close()
{
	pub_ns.publish(false);
	pub_ns.close();
}

function getSurveyString()
{
	var surveyStr:String="";
	for (var i=1; i<this.dp.length; i++)
	{
		var item:Object=this.dp[i];
		var itemStr:String=item.id+"=";
		for (var a in item.answer)
		{
			if (item.answer[a].selected)
			{
				itemStr+=a;
			}
		}
		surveyStr+=itemStr;
		if (i!=this.dp.length-1) surveyStr+="&";
	}
	return surveyStr;
}

function getSurveyItemObject(id:String)
{
	if (dp==null)	return null;
	for (var a in dp)
	{
		if (dp[a].id==Number(id))
			return dp[a];
	}
}

function updateSurveyItemObject(id:String, value:String)
{
	trace("updateSurveyItemObject...id:"+id+" value:"+value);
	var obj:Object=getSurveyItemObject(id);
	for (var a in obj.answer)
	{
		if (value.indexOf(a)>-1)
			obj.answer[a].selected=true;
		for (var b in obj.answer[a]) trace("...obj.answer["+a+"]"+"["+b+"]:"+obj.answer[a][b]);
	}
}

function initDataProvider()
{
	if (this.dp!=null) return;
	var myDp:Array=new Array();
	
	/*var survey1:Object=new Object();
	survey1.id=0;
	survey1.question="Did you ever hear of audio book?";
	survey1.allowMulti=false;
	survey1.answerCount=3;
	survey1.answer=new Object();
	survey1.answer.A=new Object();
	survey1.answer.A.label="Yes, I do!";
	survey1.answer.A.selected=false;
	survey1.answer.B=new Object();
	survey1.answer.B.label="No, I didn't.";
	survey1.answer.B.selected=true;
	survey1.answer.C=new Object()
	survey1.answer.C.label="Bullshit!";
	survey1.answer.C.selected=false;
	myDp.push(survey1);*/
	
	//Personal info
	myDp.push({id:0, question:"Your mobile phone brand?", allowMulti:false, answerCount:5,
		answer:{A:{label:"Nokia", selected:false}, B:{label:"Samsung", selected:false}, 
		C:{label:"Motolora", selected:false},D:{label:"SonyEricsson", selected:false},
		E:{label:"Other", selected:false}}});
	myDp.push({id:1, question:"Your sex?", allowMulti:false, answerCount:2,
		answer:{A:{label:"Male", selected:false}, B:{label:"Female", selected:false}}});
	myDp.push({id:2, question:"Your age?", allowMulti:false, answerCount:4,
		answer:{A:{label:"Under 18", selected:false}, B:{label:"18~25", selected:false},
		C:{label:"25~35", selected:false}, D:{label:"Above 35", selected:false}}});
	myDp.push({id:3, question:"Your other electronic product?", allowMulti:true, answerCount:4,
		answer:{A:{label:"Mp3/Mp4", selected:false}, B:{label:"PDA", selected:false},
		 C:{label:"PSP", selected:false}, D:{label:"E-book reader", selected:false}}});
	myDp.push({id:4, question:"How do you read books?", allowMulti:true, answerCount:4,
		answer:{A:{label:"Paper-based", selected:false}, B:{label:"Mobile phone", selected:false},
		C:{label:"Internet", selected:false}, D:{label:"By E-book reader", selected:false}}});
	myDp.push({id:5, question:"How long do you spent on reading every week?", allowMulti:false, answerCount:4,
		answer:{A:{label:"above 10hours", selected:false}, B:{label:"7~9hours", selected:false},
		C:{label:"4~6hours", selected:false}, D:{label:"under 3hours", selected:false}}});
	myDp.push({id:6, question:"Which types of books do you prefer?", allowMulti:true, answerCount:5,
		answer:{A:{label:"Classical", selected:false}, B:{label:"Modern", selected:false}, 
		C:{label:"Professional", selected:false},D:{label:"NetNovels", selected:false},
		E:{label:"Other", selected:false}}});
	myDp.push({id:7, question:"Which internet services do you use?", allowMulti:true, answerCount:5,
		answer:{A:{label:"Video Blogs", selected:false}, B:{label:"SNS", selected:false}, 
		C:{label:"BBS", selected:false},D:{label:"IM/Chat", selected:false},
		E:{label:"Blogs", selected:false}}});
	myDp.push({id:8, question:"Will you listen to an audio novel?", allowMulti:false, answerCount:2,
		answer:{A:{label:"Yes", selected:false}, B:{label:"No", selected:false}}});
	myDp.push({id:9, question:"By what means will you listen to an audio novel?", allowMulti:true, answerCount:5,
		answer:{A:{label:"PC", selected:false}, B:{label:"By Mobile Phone", selected:false}, 
		C:{label:"PSP", selected:false},D:{label:"MP3/MP4", selected:false},
		E:{label:"Other", selected:false}}});
	myDp.push({id:10, question:"Which types of audio book do you prefer?", allowMulti:true, answerCount:5,
		answer:{A:{label:"Chivalry", selected:false}, B:{label:"Science fiction", selected:false}, 
		C:{label:"Love", selected:false},D:{label:"Fun", selected:false},
		E:{label:"Documentary", selected:false}}});
	myDp.push({id:11, question:"When will you listen to an audio book?", allowMulti:true, answerCount:5,
		answer:{A:{label:"Way to Office", selected:false}, B:{label:"Trip", selected:false}, 
		C:{label:"Sleep", selected:false},D:{label:"Work Break", selected:false},
		E:{label:"Other", selected:false}}});
	//Product feedback
	myDp.push({id:12, question:"Will you record an audio novel?", allowMulti:false, answerCount:3,
		answer:{A:{label:"Yes", selected:false}, B:{label:"No", selected:false}, C:{label:"A little", selected:false}}});
	myDp.push({id:13, question:"How will you share your recorded audio novel?", allowMulti:false, answerCount:3,
		answer:{A:{label:"Public", selected:false}, B:{label:"Authorized", selected:false}, C:{label:"Private", selected:false}}});
	myDp.push({id:14, question:"Will you listen to an audio novel by your friend?", allowMulti:false, answerCount:2,
		answer:{A:{label:"Yes", selected:false}, B:{label:"No", selected:false}}});
	myDp.push({id:15, question:"Will you collaborate with others in recording?", allowMulti:false, answerCount:3,
		answer:{A:{label:"Yes", selected:false}, B:{label:"No", selected:false}, C:{label:"A little", selected:false}}});
	myDp.push({id:16, question:"Are you satisfied with the recording?", allowMulti:false, answerCount:3,
		answer:{A:{label:"Yes", selected:false}, B:{label:"No", selected:false}, C:{label:"A little", selected:false}}});
	myDp.push({id:17, question:"How did you feel the operation?", allowMulti:false, answerCount:4,
		answer:{A:{label:"Easy", selected:false}, B:{label:"Ok with help", selected:false},
		C:{label:"Difficult", selected:false}, D:{label:"Nightmare", selected:false}}});
	myDp.push({id:18, question:"Which difficulty did you meet?", allowMulti:false, answerCount:4,
		answer:{A:{label:"No difficulty", selected:false}, B:{label:"Information ambiguity", selected:false},
		C:{label:"Function incomplete", selected:false}, D:{label:"Help not enough", selected:false}}});
	myDp.push({id:19, question:"Which function do you feel useful?", allowMulti:true, answerCount:3,
		answer:{A:{label:"Play mode switch", selected:false}, B:{label:"Online Recording", selected:false}, C:{label:"VOD and Download", selected:false}}});
	myDp.push({id:20, question:"What is your vision using the platform?", allowMulti:false, answerCount:3,
		answer:{A:{label:"Download and Listen", selected:false}, B:{label:"Record and Share", selected:false}, C:{label:"Curiosity", selected:false}}});
	//Overall evaluation
	myDp.push({id:21, question:"Will you keep an eye on our website?", allowMulti:false, answerCount:2,
		answer:{A:{label:"Yes", selected:false}, B:{label:"No", selected:false}}});
	myDp.push({id:22, question:"What is your overall evaluation?", allowMulti:false, answerCount:4,
		answer:{A:{label:"Useless", selected:false}, B:{label:"Just so so", selected:false},
		C:{label:"Amazing", selected:false}, D:{label:"Useful for me", selected:false}}});
	myDp.push({id:23, question:"Which point mostly need improvement?", allowMulti:true, answerCount:5,
		answer:{A:{label:"Customer Service", selected:false}, B:{label:"System Response", selected:false}, 
		C:{label:"Interaction", selected:false},D:{label:"User Interface", selected:false},
		E:{label:"Functions", selected:false}}});
	
	this.dp=myDp;
}

function getDataProvider():Array
{
	return this.dp;
}

function setDataProvider(dataArr:Array)
{
	this.dp=dataArr;
}